﻿/*
 * Author:  hammercui
 * CreateTime: 2018/9/30 16:32:34
 * Module: Implement
 * 主要功能：dll方案 目前只针对ios
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;


namespace MGAudioEngine
{
    public class MGAudioEngineDllImp : IMGAudioEngine
    {

        #region DllImport
#if UNITY_IOS
        public const string WrapperLibName = "__Internal";
#elif UNITY_ANDROID || UNITY_STANDALONE_WIN
        public const string WrapperLibName = "MGAudioWrapper";
#endif

        [DllImport(WrapperLibName)]
        private static extern int init();

        [DllImport(WrapperLibName)]
        private static extern void authentication(string appId, string appKey, string deviceToken);

        [DllImport(WrapperLibName)]
        private static extern void joinRoom(long roomId, string userName);

        [DllImport(WrapperLibName)]
        private static extern void quitRoom(long roomId);

        [DllImport(WrapperLibName)]
        private static extern void createRoom(string userName);

        [DllImport(WrapperLibName)]
        private static extern void joinChannel(int channelId);

        [DllImport(WrapperLibName)]
        private static extern void reJionRoom(long roomId);

        [DllImport(WrapperLibName)]
        private static extern void sendChannelTextMessage(string msg);

        [DllImport(WrapperLibName)]
        private static extern int openMic();

        [DllImport(WrapperLibName)]
        private static extern int closeMic();

        [DllImport(WrapperLibName)]
        private static extern int openSpeaker();

        [DllImport(WrapperLibName)]
        private static extern int closeSpeaker();

        [DllImport(WrapperLibName)]
        private static extern void destroy();

        [DllImport(WrapperLibName)]
        private static extern int getIsConnect();

        [DllImport(WrapperLibName,CharSet = CharSet.Ansi)]
        private static extern IntPtr getMessage();

        #endregion



        public void AuthApp(string appId, string appKey, string deviceToken)
        {
            if(init() == 1){
                Debuger.Log("iOS init success");
                authentication(appId, appKey, deviceToken);
                MGAudioEngine.Instance.SetConnectState = ConnectState.Authenticating;
            }
            else{
                Debuger.LogError("iOS init error");
                MGAudioEngine.Instance.SetConnectState = ConnectState.Init;
            }
        }

        public void InitAduioSetting()
        {

        }

        public void UpdateAudioSetting()
        {

        }

        public void JoinRoom(long roomId,string userName)
        {
            joinRoom(roomId, userName);
        }


        public void SendTextMessage(string msg, int channelId, bool isTree)
        {
            sendChannelTextMessage(msg);
        }

        public int OpenMic()
        {
            return openMic();
        }

        public int CloseMic()
        {
            return closeMic();
        }

        public int OpenSpeaker()
        {
            return openSpeaker();
        }

        public int CloseSpeaker()
        {
            return closeSpeaker();
        }

        public void QuitRoom(long roomId)
        {
            quitRoom(roomId);
        }

        public void CreateRoom(string userName)
        {
            createRoom(userName);
        }

        public void JoinChannel(int channelId)
        {
            joinChannel(channelId);
        }

        public void ReJoinRoom(long roomId)
        {
            reJionRoom(roomId);
        }

        public void DeallocMGAudio()
        {
            destroy();
        }

        public int GetIsConnect()
        {
            return getIsConnect();
        }

        public string GetMessage()
        {
            IntPtr ptr = getMessage();
            try{

                string msg = Marshal.PtrToStringAnsi(ptr);
                return msg;
            }
            catch(Exception e){
                Debuger.LogError(e.ToString());
                return null;
            }
            //return getMessage();
        }
    }
}
